package com.wtwd.device.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wtwd.device.constant.RespCode;
import com.wtwd.device.dao.CustomerMapper;
import com.wtwd.device.model.entity.Customer;
import com.wtwd.device.model.vo.RespEntity;
import com.wtwd.device.service.CustomerService;
import com.wtwd.device.service.DeviceInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zdl
 * @since 2021-08-03
 */
@Service
public class CustomerServiceImpl implements CustomerService {

    @Resource
    private CustomerMapper customerMapper;

    @Resource
    private DeviceInfoService deviceInfoService;

    @Override
    public IPage queryPage(Page page, Customer customer) {
        return new LambdaQueryChainWrapper<>(customerMapper)
                .like(StringUtils.isNotBlank(customer.getName()), Customer::getName, customer.getName())
                .orderByDesc(Customer::getId)
                .page(page);
    }

    @Override
    public boolean save(Customer customer) {
        return customerMapper.insert(customer) == 1;
    }

    @Override
    public boolean update(Customer customer) {
        return customerMapper.updateById(customer) == 1;
    }

    @Override
    public RespEntity getAllCustomerByLikeName(String name) {
        List<Customer> customers = customerMapper.getAllCustomerByLikeName(name);
        return new RespEntity(RespCode.SUCCESS, customers);
    }

    @Override
    public boolean delete(Long customerId) {
        return customerMapper.deleteById(customerId) == 1;
    }

    @Override
    public Customer getCustomerByName(String customerName) {
        return customerMapper.getCustomerByName(customerName);
    }

    @Override
    public List<Customer> getCustomerByLikeName(String customerName) {
        if (StringUtils.isBlank(customerName)){
            return null;
        }
        return customerMapper.getAllCustomerByLikeName(customerName);
    }

    @Override
    public List<Customer> getAllCustomer() {
        return customerMapper.getAllCustomer();
    }


}
